import 'package:async/async.dart';import 'package:boss/main.dart';
import 'package:boss/models/takeout_business.dart';
import 'package:boss/ui/page/takeoutManagement/page/businessSetting/page/timeRange/model/hour_minute.dart';
import 'package:boss/ui/widget/loading/loading.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';

class BusinessSettingProvider extends ChangeNotifier {
  TakeoutBusiness takeoutBusiness;

  Future<Result<TakeoutBusiness>> initData() async {
    try {
      var res = await api.getTakeoutBusiness();
      if (!res.successful()) {
        return Result.error(res.msg);
      }

      takeoutBusiness = res.data;

      notifyListeners();

      return Result.value(res.data);
    } catch (e) {
      return Result.error(e.toString());
    }
  }

  setBusinessTime(HourMinute start, HourMinute end) {
    takeoutBusiness.takeoutBusinessHours = '${start.toString()}-${end.toString()}';
    notifyListeners();
  }

  setBusinessStatus(bool flag) {
    takeoutBusiness.businessStatus = flag ? 1 : 0;
    notifyListeners();
  }

  setAutoReceive(bool flag) {
    takeoutBusiness.autoReceive = flag ? 1 : 0;
    notifyListeners();
  }

  setTakeoutSelfMention(bool flag) {
    takeoutBusiness.takeoutSelfMention = flag ? 1 : 0;
    notifyListeners();
  }

  setStartMoney(String value) {
    try {
      takeoutBusiness.startMoney = AmountUtil.string2Price(value);
    } catch (e) {}
  }

  Future<bool> update({String successTip = '修改成功'}) async {
    try {
      showLoadingToast();

      var res = await api.updateTakeoutBusiness(takeoutBusiness);
      if (!res.successful()) {
        toast(res.msg);
        return false;
      }

      toast(successTip);

      return true;
    } catch (e) {
      toast(e.toString());
      return false;
    } finally {
      hideLoadingToast();
    }
  }
}
